package com.dahua.messagesyncbj.config.result.Exception;

import com.dahua.messagesyncbj.config.result.Code;
import com.dahua.messagesyncbj.config.result.ResultFA;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.servlet.http.HttpServletRequest;

@RestControllerAdvice
@Slf4j
public class ExceptionController {
    @ExceptionHandler(value = Exception.class)
    public ResponseEntity<?> exception(Exception e, HttpServletRequest request){
        log.warn(e.getMessage());
        return ResponseEntity.status(500)
                .body(new ResultFA().resultFA(Code.FAIL_CODE.getCode(),
                        Code.FAIL_CODE.getMessage(), e.getMessage()));
    }
    @ExceptionHandler(value = CommonException.class)
    public ResponseEntity<?> ValidatedException(CommonException e, HttpServletRequest request){
        return ResponseEntity.status(400)
                .body(new ResultFA().resultFA(Code.COMMON_EXCEPTION_CODE.getCode(),
                        Code.COMMON_EXCEPTION_CODE.getMessage(),e.getException(),e.getDescription()));
    }
}
